package com.ruoyi.ai.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.ai.domain.bo.WxBackChangeNumBo;
import com.ruoyi.ai.service.IWxNumChangeService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ai.domain.vo.WxUserVo;
import com.ruoyi.ai.domain.bo.WxUserBo;
import com.ruoyi.ai.service.IWxUserService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 微信用户B端接口
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/wxuser")
public class WxUserController extends BaseController {

    private final IWxUserService iWxUserService;

    private final IWxNumChangeService iWxNumChangeService;

    /**
     * 查询微信用户列表
     */
    @SaCheckPermission("ai:wxuser:list")
    @GetMapping("/list")
    public TableDataInfo<WxUserVo> list(WxUserBo bo, PageQuery pageQuery) {
        return iWxUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出微信用户列表
     */
    @SaCheckPermission("ai:wxuser:export")
    @Log(title = "微信用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxUserBo bo, HttpServletResponse response) {
        List<WxUserVo> list = iWxUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "微信用户", WxUserVo.class, response);
    }

    /**
     * 获取微信用户详细信息
     *
     * @param wxUserId 主键
     */
    @SaCheckPermission("ai:wxuser:query")
    @GetMapping("/{wxUserId}")
    public R<WxUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long wxUserId) {
        return R.ok(iWxUserService.queryById(wxUserId));
    }

    /**
     * 新增微信用户
     */
    @SaCheckPermission("ai:wxuser:add")
    @Log(title = "微信用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxUserBo bo) {
        return toAjax(iWxUserService.insertByBo(bo));
    }

    /**
     * 修改微信用户
     */
    @SaCheckPermission("ai:wxuser:edit")
    @Log(title = "微信用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxUserBo bo) {
        return toAjax(iWxUserService.updateByBo(bo));
    }

    /**
     * 删除微信用户
     *
     * @param wxUserIds 主键串
     */
    @SaCheckPermission("ai:wxuser:remove")
    @Log(title = "微信用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wxUserIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] wxUserIds) {
        return toAjax(iWxUserService.deleteWithValidByIds(Arrays.asList(wxUserIds), true));
    }

    /**
     * 变更AI次数
     */
    @SaCheckPermission("ai:wxuser:changeAINum")
    @Log(title = "变更AI次数", businessType = BusinessType.UPDATE)
    @PostMapping("/changeAINum")
    public R<Void> changeAINum(@Validated @RequestBody WxBackChangeNumBo wxMaConsumeBo) {
        iWxNumChangeService.changeNumFromBack(wxMaConsumeBo);
        return R.ok();
    }
}
